package com.chujian.system.auth.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.chujian.framework.exception.MsgException;
import com.chujian.framework.util.JsonUs;
import com.chujian.auth.vo.RoleInfo;
import com.chujian.auth.vo.UserAuthInfo;
import com.chujian.sys.entity.SysMenu;
import com.chujian.sys.entity.SysRoleMenu;
import com.chujian.sys.entity.SysRoleUser;
import com.chujian.sys.entity.SysUser;
import com.chujian.sys.vo.MaOp;
import com.chujian.sys.vo.menu.Menu;
import com.chujian.system.auth.service.IAuthService;
import com.chujian.system.sys.service.SysMenuService;
import com.chujian.system.sys.service.SysRoleMenuService;
import com.chujian.system.sys.service.SysRoleUserService;
import com.chujian.system.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class AuthService implements IAuthService {

    @Autowired
    private SysUserService userService;
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysRoleUserService roleUserService;
    @Autowired
    private SysRoleMenuService roleMenuService;

    @Override
    public UserAuthInfo getUserInfo(Long userId) {
        SysUser user = userService.getById(userId);
        List<RoleInfo> roleInfos = roleUserService.listUserRole(userId)
                .stream()
                .map(e -> new RoleInfo()
                        .setRoleId(e.getRoleId())
                        .setRoleName(e.getRoleName())
                        .setValue(e.getRoleId() + "")
                ).collect(Collectors.toList());

        return new UserAuthInfo()
                .setUserId(user.getUserId())
                .setUsername(user.getUserName())
                .setRealName(user.getUserName())
                .setAvatar(user.getWxHeadUrl())
                .setDesc("")
                .setRoles(roleInfos);
    }

    @Override
    public List<Menu> getAuthMenuList(Long userId, Long menuTemplateId) {
        // ## 获取所有菜单
        List<SysMenu> sysMenus = menuService.listAllRouteMenus(menuTemplateId);

        // ## 获取用户权限

        // 用户角色
        List<Long> roleIds = roleUserService.listUserRole(userId)
                .stream().map(SysRoleUser::getRoleId)
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(roleIds)) {
            throw new MsgException("用户未分配角色");
        }
        // 角色功能
        List<SysRoleMenu> roleMenus = roleMenuService.listRoleMenu(roleIds, true);

        // ## 设置权限，组合菜单
        return new RoleMenuCombiner(menuTemplateId, sysMenus, roleMenus).combine();
    }

    @Override
    public Object getAuthMaMenuList(Long userId, Long menuTemplateId) {
        List<Menu> menus = getAuthMenuList(userId, menuTemplateId);
        if (CollUtil.isEmpty(menus)) {
            return Collections.emptyList();
        }
        menus = menus.get(0).getChildren();
        return menus.stream()
                .map(e -> {
                    MaOp op = new MaOp(e.getId(), e.getPath(), e.getMeta().getIcon(), e.getMeta().getTitle(), e.getOpenType());
                    if (StringUtils.isNotEmpty(e.getExpJson())) {
                        Map<String, Object> extMap = JsonUs.toMap(e.getExpJson());
                        extMap.forEach(op::ext);
                    }
                    return op.toMap();
                }).collect(Collectors.toList());
    }

}
